/*
    Copyright (c);2019-2022;Wiscom System;

	All rights reserved.

    Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    3. Neither the name of the Wiscom System nor the names of its contributors
       may be used to endorse or promote products derived from this software without
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/

#include "logs.h"
#include "csg_adv_timer.h"
Log_obj log_array[LOG_ARRAY_LENGTH];
uint16_t log_point = 0;

void rtc_CNT_set(uint32_t cnt)
{
    Timer_CounterValueConfig(TIMER0,cnt);
}

UINT32 rtc_CNT_get(void)
{
    uint32_t temp = 0x0U;

    temp = Timer_CounterRead(TIMER0);
    return temp;
}

void init_log()
{
    /* RTC configuration */
    timer0_clk_en(ENABLE);
    timer0_set_rst(DISABLE);
    timer0_set_rst(ENABLE);

    TIMER_Init_TypeDef timer_initpara;

    /* initialize TIMER init parameter struct */
    Timer_StructParaInit(&timer_initpara);
    /* TIMER0 configuration */

    timer_initpara.prescaler = 0;

    timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection = TIMER_COUNTER_UP;
    timer_initpara.period = 0xFFFFFFFF;
    timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    Timer_Init(TIMER0, &timer_initpara);

    /* auto-reload preload enable */
    Timer_AutoReloadShadowEnable(TIMER0);
    /* auto-reload preload enable */
    Timer_Enable(TIMER0);
    /* Set RTC CNT */
    Timer_CounterValueConfig(TIMER0,0);
}

void myLog(char *p)
{
    log_array[log_point].name = p;
    log_array[log_point].tick = rtc_CNT_get();
    log_point++;
}

// void show_clock_and_exmc()
// {
//     printf("CK_SYS:%d CK_AHB:%d ",rcu_clock_freq_get(CK_SYS),rcu_clock_freq_get(CK_AHB));
//     printf("PCLK1:%d PCLK2:%d\n",rcu_clock_freq_get(CK_APB1),rcu_clock_freq_get(CK_APB2));
// //    uint16_t theDV = 2 << SPI_DV_PARAM;
// //    printf("clock_DV:(PCLK/%d)\t\t SPI0:%d\n", theDV, rcu_clock_freq_get(CK_APB2)/theDV);
// }

void show_logs()
{
    printf("\r\n======= ======= ====== Logs Start ===== ======= ======= ======= \r\n\n");
    printf("SN\t\tTime\t\tCost\t\tNote\r\n\n");
    uint16_t i=0;
    uint32_t now_time=0;
    uint32_t d_time=0;
    char ctemp;
    char *name;
    for(i=0;i<log_point;i++)
    {
        Log_obj *theLog = log_array + i;
        if(i>0)
        {        
            Log_obj *preLog = log_array + i - 1;
            uint32_t d_value = theLog->tick - preLog->tick;
            // d_time = d_value * 1.0 * 5.12;
            d_time = d_value/100;
        }
        //now_time = theLog->tick * 1.0 * 5.12;
        now_time = theLog->tick /100;
        printf("No.%d\t\t%dus\t", i+1, now_time);
        if(now_time<10000)
        {            
            printf("\t");
        }
        // 以下为显示优化，不看后缀为1的时间
        name = theLog->name;
        unsigned short int nameLen;
        for(nameLen=0;;nameLen++)
        {
        	if(name[nameLen]==0)break;
        }
        ctemp = name[nameLen - 1];
        if(ctemp=='1')
        {
        	printf("--\t\t");
        }
        else
        {
        	printf("%dus\t", d_time);
            if(d_time<10000)
            {
                printf("\t");
            }
        }
        // 以上为显示优化
        printf("%s\r\n", theLog->name);
    }
    printf("======= ======= ====== Logs Ended ===== ======= ======= ======= \r\n\n");
    // show_clock_and_exmc();
}

void userLog(Log_obj *user_log_array, UINT16 user_log_point, char *p)
{
	user_log_array[user_log_point].name = p;
    user_log_array[user_log_point].tick = rtc_CNT_get();
    user_log_point++;
}

void show_user_logs(Log_obj *user_log_array, UINT16 user_log_point)
{
    printf("\r\n======= ======= ====== Logs Start ===== ======= ======= ======= \r\n\n");
    printf("SN\t\tTime\t\tCost\t\tNote\r\n\n");
    UINT16 i=0;
    UINT32 now_time=0;
    UINT32 d_time=0;
    char ctemp;
    char *name;
    for(i=0;i<user_log_point;i++)
    {
        Log_obj *theLog = user_log_array + i;
        if(i>0)
        {
            Log_obj *preLog = user_log_array + i - 1;
            UINT32 d_value = theLog->tick - preLog->tick;
            //d_time = d_value * 1.0 * 5.12;
            d_time = d_value /100;
        }
        //now_time = theLog->tick * 1.0 * 5.12;
        now_time = theLog->tick /100;
        printf("No.%d\t\t%dus\t", i+1, now_time);
        if(now_time<10000)
        {
            printf("\t");
        }
        // 以下为显示优化，不看后缀为1的时间
        name = theLog->name;
        unsigned short int nameLen;
        for(nameLen=0;;nameLen++)
        {
        	if(name[nameLen]==0)break;
        }
        ctemp = name[nameLen - 1];
        if(ctemp=='1')
        {
        	printf("--\t\t");
        }
        else
        {
        	printf("%dus\t", d_time);
            if(d_time<10000)
            {
                printf("\t");
            }
        }
        // 以上为显示优化
        printf("%s\r\n", theLog->name);
    }
    printf("======= ======= ====== Logs Ended ===== ======= ======= ======= \r\n\n");
    // show_clock_and_exmc();
}
